package com.to.aboomy.bannersample.fragment;

import com.flyco.tablayout.SlidingTabLayout;
import com.to.aboomy.bannersample.ResourceTable;
import com.to.aboomy.bannersample.base.BaseFraction;
import com.to.aboomy.bannersample.component.BannerAndList;
import ohos.agp.components.*;

/**
 * @since 2021-07-21
 */
public class Test2Fragment  extends BaseFraction{
    private static final String[] mTitles = new String[]{"NOUGAT", "M", "KITKAT", "JELLY_BEAN", "ICE_CREAM_SANDWICH", "HONEYCOMB", "GINGERBREAD", "ECLAIR", "DONUT", "CUPCAKE"};
    PageSlider pageSlider;
    ScrollView scrollView1;

    @Override
    public int getUiContent() {
        return ResourceTable.Layout_fragment_test1;
    }

    @Override
    public void initComponent() {
        pageSlider = (PageSlider) mComponent.findComponentById(ResourceTable.Id_pageslider);
        scrollView1 = (ScrollView) mComponent.findComponentById(ResourceTable.Id_scrollview);
        initPageSlider();
    }

    private void initPageSlider(){
        MyPagerAdapter myPagerAdapter = new MyPagerAdapter(mTitles);
        pageSlider.setProvider(myPagerAdapter);
        SlidingTabLayout slidingTabLayout = (SlidingTabLayout) mComponent.findComponentById(ResourceTable.Id_slidingtab);
        SlidingTabLayout.SlidingTabLayoutConfig slidingTabLayoutConfig =
                slidingTabLayout.new SlidingTabLayoutConfig();
        slidingTabLayoutConfig.indicatorGravity = SlidingTabLayout.GRAVITY_BOTTOM;
        slidingTabLayoutConfig.textBold = SlidingTabLayout.TEXT_BOLD_NONE;
        slidingTabLayoutConfig.indicatorWidthEqualTitle = true;
        slidingTabLayoutConfig.textSelectColor = 0xffff5454;
        slidingTabLayoutConfig.textUnselectColor = 0xff000000;
        slidingTabLayoutConfig.indicatorHeight = 3;
        slidingTabLayoutConfig.indicatorColor = 0xffff5454;
        slidingTabLayoutConfig.textsize = 14;
        slidingTabLayout.setScrollView(scrollView1);
        slidingTabLayout.setConfig(slidingTabLayoutConfig);
        slidingTabLayout.setViewPager(pageSlider, mTitles);
    }


    private class MyPagerAdapter extends PageSliderProvider {
        String[] titles;

        private MyPagerAdapter(String[] titles) {
            this.titles = titles;
        }

        @Override
        public int getCount() {
            if (titles == null) {
                return 0;
            } else {
                return titles.length;
            }
        }

        @Override
        public Object createPageInContainer(ComponentContainer componentContainer, int i) {
            BannerAndList bannerAndList = new BannerAndList(mComponent.getContext());
            bannerAndList.setLayoutConfig(new DirectionalLayout.LayoutConfig(DirectionalLayout.LayoutConfig.MATCH_PARENT,DirectionalLayout.LayoutConfig.MATCH_PARENT));
            componentContainer.addComponent(bannerAndList);
            return componentContainer;
        }

        @Override
        public void destroyPageFromContainer(ComponentContainer componentContainer, int i, Object o) {
            componentContainer.removeAllComponents();
        }

        @Override
        public boolean isPageMatchToObject(Component component, Object o) {
            return false;
        }
    }
}

